import java.util.Enumeration;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.sun.messaging.jmq.jmsserver.core.Session;


public class Main {
	public static void main(String[] args) {

		new Main().getMessages();
	}

	public void getMessages() {
		Context jndiContext = null;
		ConnectionFactory connectionFactory = null;
		Queue queue = null;
		try {
			jndiContext = new InitialContext();
		} catch (NamingException e) {
			System.out.println("nu putem crea contextul JNDI API "
					+ e.getMessage());
		}
		try {
			connectionFactory = (QueueConnectionFactory) jndiContext
					.lookup("jms/Firstjms");
			queue = (Queue) jndiContext.lookup("jms/Firstjmsqueue");
		} catch (NamingException e) {
			System.out.println("cautare esuata in JNDI API: " + e.getMessage());
		}

		Connection connection;
		Enumeration<?> messageEnumeration;
		TextMessage textMessage;
		

	
			try {
				connection = connectionFactory.createConnection();
			

			javax.jms.Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			
			QueueBrowser browser = session.createBrowser(queue);
			
			messageEnumeration = browser.getEnumeration();
			
			if(messageEnumeration != null) {
				if(!messageEnumeration.hasMoreElements())
				{
					System.out.println("nu sunt mesaje in coada.");
				}
				else
				{
					System.out.println("in coada avem urmatoarele mesaje:");
					while(messageEnumeration.hasMoreElements())
					{
						textMessage = (TextMessage) messageEnumeration.nextElement();
						System.out.println(textMessage.getText());
					}
				}
			}
				
	
	} catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
			
			
			
			
	public Main() {
		super();
	}

}